package com.quanshu.shop.person.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.order.entity.SubmitPrepareEntity;
import com.quanshu.shop.person.custom.AdressPickerDialog;
import com.quanshu.shop.person.entity.AddressBaseEntity;
import com.quanshu.shop.person.entity.AdressResultEntity;
import com.quanshu.shop.person.entity.MySelectAddressEntity;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.ielse.view.SwitchView;

public class AddAddressActivity extends BaseActivity implements AdressPickerDialog.OnGetAddressListener {

    @BindView(R.id.add_address_title)
    BaseTitleBar addAddressTitle;
    @BindView(R.id.add_address_select)
    LinearLayout addAddressSelect;
    @BindView(R.id.add_address_edit_name)
    EditText addAddressEditName;
    @BindView(R.id.add_address_edit_phone)
    EditText addAddressEditPhone;
    @BindView(R.id.add_address_select_text)
    TextView addAddressSelectText;
    @BindView(R.id.add_address_edit_info)
    EditText addAddressEditInfo;
    @BindView(R.id.add_address_ok)
    RelativeLayout addAddressOk;
    @BindView(R.id.add_address_default)
    SwitchView addAddressDefault;
    private AdressPickerDialog adressPickerDialog;
    private AddressBaseEntity.DataEntity mProvince, mCity, mArea;
    private MySelectAddressEntity mySelectAddressEntity;
    private Bundle bundle;
    public static final int ADDRESS_RESULT_CODE = 20001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).titleBar(addAddressTitle).init();
        initView();
    }

    private void initView() {
        bundle = getIntent().getExtras();
        adressPickerDialog = new AdressPickerDialog(this);
        adressPickerDialog.setOnGetAddressListener(this);
        RxViewHelper.getInstance().clicks(addAddressSelect, aVoid ->
                adressPickerDialog.show());
        RxViewHelper.getInstance().clicks(addAddressOk, aVoid ->
                addAddress());
        if (bundle != null) {
            addAddressTitle.setCenterText(bundle.getString("title", "添加地址")).setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
                @Override
                public void onclickLeft() {
                    finish();
                }

                @Override
                public void onclickRight() {

                }
            });
            if (bundle!=null&&Integer.parseInt(bundle.getString("request_code")) == SelectMyAddressActivity.EDIT_ADDRESS) {
                mySelectAddressEntity = (MySelectAddressEntity) bundle.getSerializable("request_data");
                addAddressEditName.setText(mySelectAddressEntity.getAddressListEntity().getAccept_name());
                addAddressEditPhone.setText(mySelectAddressEntity.getAddressListEntity().getMobile());
                addAddressSelectText.setText(mySelectAddressEntity.getAddressListEntity().getProvince_val() + mySelectAddressEntity.getAddressListEntity().getCity_val() + mySelectAddressEntity.getAddressListEntity().getArea_val());
                addAddressEditInfo.setText(mySelectAddressEntity.getAddressListEntity().getAddress());
                mProvince = new AddressBaseEntity.DataEntity();
                mProvince.setArea_id(mySelectAddressEntity.getAddressListEntity().getProvince());
                mProvince.setArea_name(mySelectAddressEntity.getAddressListEntity().getProvince_val());
                mCity = new AddressBaseEntity.DataEntity();
                mCity.setArea_id(mySelectAddressEntity.getAddressListEntity().getCity());
                mCity.setArea_name(mySelectAddressEntity.getAddressListEntity().getCity_val());
                mArea = new AddressBaseEntity.DataEntity();
                mArea.setArea_id(mySelectAddressEntity.getAddressListEntity().getArea());
                mArea.setArea_name(mySelectAddressEntity.getAddressListEntity().getArea_val());
                if (Integer.parseInt(mySelectAddressEntity.getAddressListEntity().getIs_default()) == 1) {
                    addAddressDefault.setOpened(true);
                } else {
                    addAddressDefault.setOpened(false);
                }
            }
        } else {
            addAddressTitle.setCenterText("添加地址").setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
                @Override
                public void onclickLeft() {
                    finish();
                }

                @Override
                public void onclickRight() {

                }
            });
            addAddressDefault.setOpened(true);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    private void addAddress() {
        if (TextUtils.isEmpty(addAddressEditName.getText())) {
            ToastHelper.toast("请输入收货人姓名");
            return;
        }
        if (TextUtils.isEmpty(addAddressEditPhone.getText())) {
            ToastHelper.toast("请输入联系方式");
            return;
        }
        if (mProvince == null) {
            ToastHelper.toast("请选择省市区");
            return;
        }
        if (TextUtils.isEmpty(addAddressEditInfo.getText())) {
            ToastHelper.toast("请输入详细地址");
            return;
        }
        String isDefault = addAddressDefault.isOpened() ? "1" : "0";
        String id = mySelectAddressEntity == null ? "" : mySelectAddressEntity.getAddressListEntity().getId();
        Log.d("111111", "addAddress: " + id);
        NetClient.getInstance().getShopService(ShopMainIF.class)
                .addAddress(AppUtils.getSignParams(currentUser.getId(), id, addAddressEditName.getText().toString(), addAddressEditPhone.getText().toString(), mProvince.getArea_id(), mCity.getArea_id(), mArea.getArea_id(), addAddressEditInfo.getText().toString(), isDefault), currentUser.getId(), id, addAddressEditName.getText().toString(), addAddressEditPhone.getText().toString(), mProvince.getArea_id(), mCity.getArea_id(), mArea.getArea_id(), addAddressEditInfo.getText().toString(), isDefault)
                .compose(new BaseTransformer<>(this, true, true))
                .subscribe(new BaseObserver<BaseResponse<AdressResultEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<AdressResultEntity> response) {
                        Intent intent = new Intent();
                        Bundle bundle1 = new Bundle();
                        if (Integer.parseInt(bundle.getString("request_code")) == SelectMyAddressActivity.EDIT_ADDRESS) {
                            mySelectAddressEntity.getAddressListEntity().setIs_default(isDefault);
                            mySelectAddressEntity.getAddressListEntity().setAccept_name(addAddressEditName.getText().toString());
                            mySelectAddressEntity.getAddressListEntity().setMobile(addAddressEditPhone.getText().toString());
                            mySelectAddressEntity.getAddressListEntity().setProvince(mProvince.getArea_id());
                            mySelectAddressEntity.getAddressListEntity().setProvince_val(mProvince.getArea_name());
                            mySelectAddressEntity.getAddressListEntity().setCity(mCity.getArea_id());
                            mySelectAddressEntity.getAddressListEntity().setCity_val(mCity.getArea_name());
                            mySelectAddressEntity.getAddressListEntity().setArea(mArea.getArea_id());
                            mySelectAddressEntity.getAddressListEntity().setArea_val(mArea.getArea_name());
                            mySelectAddressEntity.getAddressListEntity().setAddress(addAddressEditInfo.getText().toString());
                            ToastHelper.toast("修改成功！");
                        } else if (Integer.parseInt(bundle.getString("request_code")) == SelectMyAddressActivity.ADD_ADDRESS) {
                            mySelectAddressEntity = new MySelectAddressEntity();
                            SubmitPrepareEntity.AddressListEntity addressListEntity = new SubmitPrepareEntity.AddressListEntity();
                            mySelectAddressEntity.setAddressListEntity(addressListEntity);
                            mySelectAddressEntity.getAddressListEntity().setIs_default(isDefault);
                            mySelectAddressEntity.getAddressListEntity().setAccept_name(addAddressEditName.getText().toString());
                            mySelectAddressEntity.getAddressListEntity().setMobile(addAddressEditPhone.getText().toString());
                            mySelectAddressEntity.getAddressListEntity().setProvince(mProvince.getArea_id());
                            mySelectAddressEntity.getAddressListEntity().setProvince_val(mProvince.getArea_name());
                            mySelectAddressEntity.getAddressListEntity().setCity(mCity.getArea_id());
                            mySelectAddressEntity.getAddressListEntity().setCity_val(mCity.getArea_name());
                            mySelectAddressEntity.getAddressListEntity().setArea(mArea.getArea_id());
                            mySelectAddressEntity.getAddressListEntity().setArea_val(mArea.getArea_name());
                            mySelectAddressEntity.getAddressListEntity().setAddress(addAddressEditInfo.getText().toString());
                            mySelectAddressEntity.getAddressListEntity().setId(response.getData().getId());
                            ToastHelper.toast("添加成功！");
                        }
                        bundle1.putSerializable("result_data", mySelectAddressEntity);
                        intent.putExtras(bundle1);
                        setResult(ADDRESS_RESULT_CODE, intent);
                        finish();
                    }
                });
    }

    @Override
    public void selectAddress(AddressBaseEntity.DataEntity province, AddressBaseEntity.DataEntity city, AddressBaseEntity.DataEntity area) {
        Log.d("选择地址", "selectAddress: " + province.getArea_name());
        Log.d("选择地址", "selectAddress: " + city.getArea_name());
        Log.d("选择地址", "selectAddress: " + area.getArea_name());
        mProvince = province;
        mCity = city;
        mArea = area;
        if ("县".equals(city.getArea_name()) || "市辖区".equals(city.getArea_name())) {
            addAddressSelectText.setText(province.getArea_name() + area.getArea_name());
        } else {
            addAddressSelectText.setText(province.getArea_name() + city.getArea_name() + area.getArea_name());
        }

    }
}
